/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.config;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import org.fba.prionace.file.JFile;

public class ShortcutStorage {

	public static void save(List<JFile> shortcuts) throws FileNotFoundException {
		File output = new File(System.getProperty("user.home")
				.concat("/.prionace/config/shortcuts.xml")
				.replace('/', File.separatorChar));

		if (!output.exists()) {
			output.getParentFile().mkdirs();
		}

		Vector<String> pathList = new Vector<String>();
		XMLEncoder xml = new XMLEncoder(new BufferedOutputStream(
				new FileOutputStream(output)));

		for (JFile shortcut : shortcuts) {
			pathList.add(shortcut.getFile().getAbsolutePath());
		}

		xml.writeObject(pathList);
		xml.flush();
		xml.close();
	}

	@SuppressWarnings("unchecked")
	public static Vector<String> restore() throws IOException {
		Vector<String> restoreList = new Vector<String>();
		File input = new File(System.getProperty("user.home")
				.concat("/.prionace/config/shortcuts.xml")
				.replace('/', File.separatorChar));

		if (input.exists()) {
			XMLDecoder xml = null;

			try {
				xml = new XMLDecoder(new BufferedInputStream(
						new FileInputStream(input)));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

			if (xml != null) {
				try {
					restoreList = (Vector<String>) xml.readObject();
				} catch (Exception e) {
					throw new IOException(
							"Arquivo shortcuts.xml foi corrompido.");
				} finally {
					xml.close();
				}
			}
		}

		return restoreList;
	}
}
